﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniBottles.Models;
using MiniBottles.Repositories;
using System.IO;

namespace MiniBottles.Controllers
{
    [Authorize]
    public class BottleController : Controller
    {
        private DataContext db = new DataContext();
        private IDictCollectionRepository dictCollectionRepository;

        public BottleController()
        {
            this.dictCollectionRepository = new DictCollectionRepository(db);
        }



        //
        // GET: /Bottle/

        public ActionResult Index(int categoryId = 0, int collectionId = 0)
        {
            List<Bottle> result;
            if (categoryId > 0)
            {
                result = db.Bottles.Where(x => !x.IsDeleted && x.DictCategory.Id == categoryId).ToList();
            }
            else if (collectionId > 0)
            {
                result = db.Bottles.Where(x => !x.IsDeleted && x.DictCategory.DictCollection.Id == collectionId).ToList();
            }
            else
            {
                result = db.Bottles.Where(x => !x.IsDeleted).ToList();
            }

            return View(result);
        }

        //
        // GET: /Bottle/Details/5

        public ActionResult Details(int id = 0)
        {
            Bottle bottle = db.Bottles.Find(id);
            if (bottle == null)
            {
                return HttpNotFound();
            }
            return View(bottle);
        }

        //
        // GET: /Bottle/Create

        public ActionResult Create(int categoryId = 0)
        {
            ViewBag.DictCollections = dictCollectionRepository.GetAll();
            Bottle bottle = new Bottle();
            bottle.DictCategory = db.DictCategory.Find(categoryId);
            return View(bottle);
        }

        //
        // POST: /Bottle/Create

        [HttpPost]
        public ActionResult Create(Bottle bottle, HttpPostedFileBase file, int categoryId = 0 )
        {
            bottle.DictCategory = db.DictCategory.Find(categoryId);

            ValidateBottle(bottle);

            if (ModelState.IsValid)
            {

                var path = Path.Combine(Server.MapPath("~/Upload/"));
                bottle.FileName = Helper.SaveFiles(file, path);
                

                db.Bottles.Add(bottle);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DictCollections = dictCollectionRepository.GetAll();

            return View(bottle);
        }


        private void ValidateBottle(Bottle bottle)
        {
            if (bottle.DictCategory != null)
            {
                ModelState.Remove("DictCategory");
            }
        }
        //
        // GET: /Bottle/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ViewBag.DictCollections = dictCollectionRepository.GetAll();
            Bottle bottle = db.Bottles.Find(id);
            if (bottle == null)
            {
                return HttpNotFound();
            }
            return View(bottle);
        }

        //
        // POST: /Bottle/Edit/5

        [HttpPost]
        public ActionResult Edit(Bottle bottle, HttpPostedFileBase file, int categoryId = 0)
        {

            bottle.DictCategory = db.DictCategory.Find(categoryId);
            ValidateBottle(bottle);

            if (ModelState.IsValid)
            {
                Bottle bottleDb = db.Bottles.Find(bottle.Id);

                var path = Path.Combine(Server.MapPath("~/Upload/"));

                bottleDb.DictCategory = bottle.DictCategory;
                bottleDb.Name = bottle.Name;
                bottleDb.IsActive = bottle.IsActive;
                bottleDb.Material = bottle.Material;
                bottleDb.Alcohol = bottle.Alcohol;
                bottleDb.Capacity = bottle.Capacity;
                bottleDb.Country = bottle.Country;
                bottleDb.Description = bottle.Description;
                bottleDb.ToExchange = bottle.ToExchange;
                bottleDb.IsFavorite = bottle.IsFavorite;
                bottleDb.IsWanted = bottle.IsWanted;
                bottleDb.Order = bottle.Order;
                if (file != null)
                {
                    bottleDb.FileName = Helper.SaveFiles(file, path);
                }

                db.Entry(bottleDb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DictCollections = dictCollectionRepository.GetAll();
            return View(bottle);
        }

        //
        // GET: /Bottle/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Bottle bottle = db.Bottles.Find(id);
            if (bottle == null)
            {
                return HttpNotFound();
            }
            ViewBag.DictCollections = dictCollectionRepository.GetAll();
            return View(bottle);
        }

        //
        // POST: /Bottle/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Bottle bottle = db.Bottles.Find(id);

            if (bottle.DictCategory != null)
            {
                ModelState.Remove("DictCategory");
            }

            //db.Bottles.Remove(bottle);
            bottle.IsDeleted = true;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}